/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sistemperpustakaan.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.view.JasperViewer;
import sistemperpustakaan.entity.Buku;
import sistemperpustakaan.entity.Inventaris;
import sistemperpustakaan.entity.ViewInventaris;
import sistemperpustakaan.model.BukuModel;
import sistemperpustakaan.model.InventarisModel;
import sistemperpustakaan.tablemodel.InventarisTableModel;
import sistemperpustakaan.view.FrmMain;
import sistemperpustakaan.view.buku.DialogInventarisDetail;
import sistemperpustakaan.view.buku.FrmInventaris;

/**
 *
 * @author EKo
 */
public class InventarisController {

    FrmMain frameMain;
    FrmInventaris inventarisView;
    DialogInventarisDetail inventarisDetail;
    InventarisModel model;
    Inventaris inventaris;
    ViewInventaris vInventaris;

    public InventarisController() {
    }

    public InventarisController(FrmInventaris inventarisView, InventarisModel model) {
        this.inventarisView = inventarisView;
        this.model = model;
    }

    public FrmInventaris getInventarisView() {
        return inventarisView;
    }

    public void setInventarisView(FrmInventaris inventarisView) {
        this.inventarisView = inventarisView;
    }

    public DialogInventarisDetail getInventarisDetail() {
        return inventarisDetail;
    }

    public void setInventarisDetail(DialogInventarisDetail inventarisDetail) {
        this.inventarisDetail = inventarisDetail;
    }

    public FrmMain getFrameMain() {
        return frameMain;
    }

    public void setFrameMain(FrmMain frameMain) {
        this.frameMain = frameMain;
    }

    public void fillComboPenerbit(JComboBox comboBox) {
        comboBox.setModel(new DefaultComboBoxModel(model.getVectorPenerbit()));
    }

    public void findListInventaris() {
        inventarisView.setTableModel(new InventarisTableModel());
        inventarisView.getTableModel().setListInventaris(model.getListInventaris(
                inventarisView.getTxtInventarisId().getText(),
                inventarisView.getTxtBukuId().getText(),
                inventarisView.getTxtNoPanggil().getText(),
                inventarisView.getTxtIsbn().getText(),
                inventarisView.getTxtJudul().getText(),
                inventarisView.getTxtPengarang().getText(),
                inventarisView.getComboPenerbit().getSelectedItem().toString(),
                inventarisView.getOpt()));
        inventarisView.getTblInventaris().setModel(inventarisView.getTableModel());
        inventarisView.setTableView();
        inventarisView.setInventarisId("KI000000");
        int jumlah = inventarisView.getTblInventaris().getRowCount();
        inventarisView.getLblJumlah().setText(Integer.toString(jumlah));
    }

    public void refreshListInventaris() {
        inventarisView.getTxtInventarisId().setText("");
        inventarisView.getTxtBukuId().setText("");
        inventarisView.getTxtNoPanggil().setText("");
        inventarisView.getTxtJudul().setText("");
        inventarisView.getTxtPengarang().setText("");
        inventarisView.getTxtIsbn().setText("");
        inventarisView.getComboPenerbit().setSelectedIndex(0);
        inventarisView.getRadioSemua().setSelected(true);
        inventarisView.setOpt(FrmInventaris.PILIH_SEMUA);
        findListInventaris();
    }

    public void saveDeleteInventaris() {
        if (inventarisView.getInventarisId() == "KI000000") {
            JOptionPane.showMessageDialog(inventarisView, "Anda belum memilih data");
        } else if (JOptionPane.showConfirmDialog(inventarisView, "Apakah anda yakin menghapus data ini?", "Hapus data",
                JOptionPane.YES_NO_CANCEL_OPTION) == JOptionPane.YES_OPTION) {
            if (model.deleteInventaris(inventarisView.getInventarisId())) {
                JOptionPane.showMessageDialog(inventarisView, "Delete data inventaris sukses");
                findListInventaris();
            } else {
                JOptionPane.showMessageDialog(inventarisView, "Delete data inventaris gagal");
            }
        }
    }

    public void showDetailInventaris(String id) {
        inventaris = model.getInventaris(id);
        inventarisDetail.getTxtIdInventaris().setText(inventaris.getId());
        inventarisDetail.getTxtIdBuku().setText(inventaris.getBuku_id()+"");
        setSelectedBuku(inventaris.getBuku_id());
        inventarisDetail.getTxtStatus().setText(inventaris.getStatus() + "");

    }

    public void setSelectedBuku(int idBuku) {
//        System.out.println(idBuku);
        Buku buku = new BukuModel().getBuku(idBuku);
        inventarisDetail.getTxtIdBuku().setText(buku.getId()+"");
        inventarisDetail.getTxtJudul().setText(buku.getJudul());
        inventarisDetail.getTxtNoPanggil().setText(buku.getNo_panggil());
        inventarisDetail.getTxtPengarang().setText(buku.getPengarang());
        
    }
//    public void setSelectedStatus(int status) {
//        Bahasa bahasa = model.getBahasa(status);
//        inventarisDetail.getTxtStatus().setText(bahasa.getId() + "");
//        inventarisDetail.getComboStatus().setSelectedIndex(idBahasa);
//    }
    //disiniiiiiii

    public void saveNewInventaris() {
        if (inventarisDetail.getTxtIdInventaris().getText().equals("")) {
            JOptionPane.showMessageDialog(inventarisView, "ID inventaris masih kosong");
        } else if (inventarisDetail.getTxtIdBuku().getText().equals("")) {
            JOptionPane.showMessageDialog(inventarisView, "ID buku masih kosong");
        }  else {
            if (model.insertInventaris(inventarisDetail.getTxtIdInventaris().getText(),inventarisDetail.getTxtIdBuku().getText(),Integer.valueOf(inventarisDetail.getTxtStatus().getText()))) {
                JOptionPane.showMessageDialog(inventarisView, "Simpan data inventaris sukses");
                inventarisDetail.dispose();
                findListInventaris();
            } else {
                JOptionPane.showMessageDialog(inventarisView, "Simpan data inventaris gagal");
            }
        }
    }

    public void saveUpdateInventaris() {
       if (inventarisDetail.getTxtIdInventaris().getText().equals("")) {
            JOptionPane.showMessageDialog(inventarisView, "ID inventaris masih kosong");
        } else if (inventarisDetail.getTxtIdBuku().getText().equals("")) {
            JOptionPane.showMessageDialog(inventarisView, "ID buku masih kosong");
        }  else {
            if (model.updateInventaris(inventarisDetail.getTxtIdInventaris().getText(),inventarisDetail.getTxtIdBuku().getText(),Integer.valueOf(inventarisDetail.getTxtStatus().getText()), inventarisView.getInventarisId() )) {
                JOptionPane.showMessageDialog(inventarisView, "Update data inventaris sukses");
                inventarisDetail.dispose();
                findListInventaris();
            } else {
                JOptionPane.showMessageDialog(inventarisView, "Update data inventaris gagal");
            }
        }
    }
    public void showGenerateIdInventaris() {
        Inventaris inventais = model.generateIdInventaris();
        inventarisDetail.getTxtIdInventaris().setText(inventais.getId() + "");
    }
    public void report(int opt) {
//        File report_file = new File("report/BukuReport.jasper");
        InputStream stream = getClass().getResourceAsStream("report/InventarisReport.jasper");
        Map<String, Object> map = new HashMap<String, Object>();


//
        map.put("inventaris_id", inventarisView.cekInventaris_id());
        map.put("buku_id", inventarisView.cekBuku_id());
        map.put("no_panggil", inventarisView.cekNoPanggil());
        map.put("isbn", inventarisView.cekIsbn());
        map.put("judul", inventarisView.cekJudul());
        map.put("pengarang", inventarisView.cekPengarang());
        map.put("penerbit", inventarisView.cekPenerbit());
        map.put("opt", inventarisView.cekOpt());
        map.put("logo", "C:\\logo.jpg");

        try {
//            System.out.println("ngisor catch");
//            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(report_file.getPath());
            JasperPrint jasperPrint = JasperFillManager.fillReport(stream, map, model.getConnection());
            switch (opt) {

                case 0:
                    JasperViewer.viewReport(jasperPrint, false);
//                    System.out.println("mlebu opt");
                    break;
                case 1:
                    JasperPrintManager.printReport(jasperPrint, true);
                    break;
//                case 2:
//                            JFileChooser fc = new JFileChooser();
//                            fc.setFileFilter(new PdfFilter());
//                            int returnValue = fc.showSaveDialog(anggotaView);
//                            if(returnValue == JFileChooser.APPROVE_OPTION){
//                                File f = fc.getSelectedFile();
//                                String path = f.getPath();
//                                String ext = FilterUtility.getExtension(f);
//                                if(ext != "pdf" ) path += ".pdf";
//                                System.out.println("sukses");
////                                JasperPrintManager.printReport(, true);
//                                JasperPrintManager.printReportToPdfFile(jasperPrint, path);
//                                JOptionPane.showMessageDialog(anggotaView, "Downlaod data berhasil \n lokasi file anda: "+path);
//                            }
//                            break;
            }

        } catch (JRException ex) {
            JOptionPane.showMessageDialog(null, "Data tidak dapat dicetak!" + "\n" + ex.getMessage(), "Cetak Data", JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
        }
    }
}
